package com.lanxin.oa.fiancial.dao;
import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.*;
import com.lanxin.util.PageBean;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Repository
public class ApplyDaoImpl extends BaseDaoImpl implements ApplyDao {
    @Override
    public PageBean<BxInformal> selectBxInformalPage(HttpSession session, Integer page, Integer pageSize,BxInformal bxInformal) {
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        Integer empid = (Integer) session.getAttribute("empid");
        if (page==null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        String hql=("from BxInformal b left join fetch b.empcreator left join fetch b.empproposer where b.creator="+empid);
        if(bxInformal.getDocNo()!=null && !"".equals(bxInformal.getDocNo())){
            hql=hql+" and b.docNo like '%"+bxInformal.getDocNo()+"%'";
        }
        if(bxInformal.getStatus()!=null && !"".equals(bxInformal.getStatus())){
            hql=hql+" and b.status='"+bxInformal.getStatus()+"'";
        }
        if(bxInformal.getSubmitDate()!=null){
            hql=hql+" and b.submitDate like '%"+sim.format(bxInformal.getSubmitDate())+"%'";
        }
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public List<BxInformaltype> selectBxType() {
        return queryAll("from BxInformaltype where isActice=1");
    }

    @Override
    public BxInformal selectByBxInFormalId(Integer oid) {
        return (BxInformal) selectByHQL("from BxInformal b left join fetch b.emppayee  left join fetch b.empbooker left join fetch b.empcreator left join fetch b.empproposer left join fetch b.dept left join fetch b.lcflow left join fetch b.maldetails m left join fetch m.bxtype left join fetch m.bxinfo left join fetch b.malaccessary where b.oid="+oid);
    }

    @Override
    public List<LcFlow> selectLcFlow() {
        return queryAll("select new LcFlow(l.oid,l.flowName) from LcFlow l where l.state='Y'");
    }

    @Override
    public BxInformal selectbx(Integer oid) {
        return (BxInformal) selectByHQL("from BxInformal b left join fetch b.maldetails where b.oid="+oid);
    }

    @Override
    public BxInformal deleteBxInformal(Integer oid) {
        return (BxInformal) selectByHQL("from BxInformal where oid="+oid);
    }

    @Override
    public PageBean<BxInformal> listBxInformalPage(Integer page, Integer pageSize, BxInformal bxInformal,String empName) {
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        if (page==null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        String hql=("from BxInformal b left join fetch b.empcreator left join fetch b.empproposer where b.status!='W'");
        if(bxInformal.getDocNo()!=null && !"".equals(bxInformal.getDocNo())){
            hql=hql+" and b.docNo like '%"+bxInformal.getDocNo()+"%'";
        }
        if(bxInformal.getStatus()!=null && !"".equals(bxInformal.getStatus())){
            hql=hql+" and b.status='"+bxInformal.getStatus()+"'";
        }
        if(bxInformal.getSubmitDate()!=null){
            hql=hql+" and b.submitDate like '%"+sim.format(bxInformal.getSubmitDate())+"%'";
        }
        if(empName!=null && !"".equals(empName)){
            hql=hql+" and b.empproposer.empName='"+empName+"'";
        }
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public PageBean<BxInformal> listregisterPage(Integer page, Integer pageSize, BxInformal bxInformal,String empName) {
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        if (page==null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        String hql=("from BxInformal b left join fetch b.empcreator left join fetch b.empproposer where b.status='Y' ");
        if(bxInformal.getDocNo()!=null && !"".equals(bxInformal.getDocNo())){
            hql=hql+" and b.docNo like '%"+bxInformal.getDocNo()+"%'";
        }
        if(bxInformal.getStatus()!=null && !"".equals(bxInformal.getStatus())){
            hql=hql+" and b.status='"+bxInformal.getStatus()+"'";
        }
        if(bxInformal.getSubmitDate()!=null){
            hql=hql+" and b.submitDate like '%"+sim.format(bxInformal.getSubmitDate())+"%'";
        }
        if(empName!=null && !"".equals(empName)){
            hql=hql+" and b.empproposer.empName='"+empName+"'";
        }
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public ZzEmployee selectEmp(Integer proposer) {
        return (ZzEmployee) selectByHQL("from ZzEmployee z where z.oid="+proposer);
    }

    @Override
    public LcFlow selectByIdLcFlow(Integer flowid) {
        return (LcFlow) selectByHQL("from LcFlow l where l.oid="+flowid);
    }

}
